package com.example.vietvoicecmdsimple;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.view.inputmethod.InputBinding;

public class VParameter {
	
	public enum FormatType{
		TIME, NUMBER, CONTACT, TEXT, APPLICATION
	}
	
	public String Value;
	public FormatType Format;
	public String Question;
	public Boolean IsNeedAsk = false;
	
	public static ContentResolver cr;
	
	public Boolean Validate(){
		switch(Format){
		case NUMBER:
			return Value.matches("[0-9 ]+");
		case TIME:
			Value = Value.trim().replaceAll("\\s+", " ");
			String[] sS = Value.split(" ");
			int hours = 0, mins = 0, day = 0, month = 0;
			try{
				for (int i = 0; i < sS.length; i++){
					if (sS[i].equals("giờ")){
						if (i > 0){
							hours = Integer.parseInt(sS[i-1]);
						}
					}
					if (sS[i].equals("phút")){
						if (i > 0){
							mins = Integer.parseInt(sS[i-1]);
						}
					}
					if (sS[i].equals("ngày")){
						if (i + 1 < sS.length){
							day = Integer.parseInt(sS[i+1]);
						}
					}
					if (sS[i].equals("tháng")){
						if (i + 1 < sS.length){
							month = Integer.parseInt(sS[i+1]);
						}
					}
				}
			}
			catch (Exception e) {
				hours = mins = 0;
			}
			if (hours == 0 && mins == 0)
			{
				return false;
			}
			else{
				Value = hours + ":" + mins + ":" + day + ":" + month;
				return true;
			}
		case CONTACT:
			if (Value.matches("[0-9 ]+"))
				return true;
			
			Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
            // use the cursor to access the contacts 
			while (phones.moveToNext())
		    {
		      String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
		             // get display name
		      String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
		              // get phone number
		      System.out.println(name + ".................."+phoneNumber);
		      if (name.toLowerCase().contains(Value.toLowerCase())){
		    	  Value = phoneNumber;
		    	  return true;
		      }
		    }
			return false;
		case TEXT:
			return !Value.isEmpty();
		case APPLICATION:
			return !Value.isEmpty();
		default:
			return false;
		}
		return false;
	}
}
